/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnFrame.java
 *
 * Created on 21/11/2011, 16:32:58
 */

package proj5labbd.graphics;

import proj5labbd.exceptions.NullConnection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/*
 * Interface gráfica de conexão, utilizada para a conexão com o SGBD
 */
public class ConnFrame extends javax.swing.JFrame {

    private MainFrame main;
    /** Creates new form ConnFrame */
    public ConnFrame(MainFrame main) {
        initComponents();
        ConnAction act = new ConnAction();
        jbtConnect.addActionListener(act);
        jbtConnCancel.addActionListener(act);
        this.main = main;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpanelConn = new javax.swing.JPanel();
        jlbIp = new javax.swing.JLabel();
        jtxtIp = new javax.swing.JTextField();
        jlbPort = new javax.swing.JLabel();
        jtxtPort = new javax.swing.JTextField();
        jlbUser = new javax.swing.JLabel();
        jtxtUser = new javax.swing.JTextField();
        jlbPass = new javax.swing.JLabel();
        jpassConn = new javax.swing.JPasswordField();
        jbtConnect = new javax.swing.JButton();
        jbtConnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Connection");
        setName("jfrmConn"); // NOI18N

        jpanelConn.setBorder(javax.swing.BorderFactory.createTitledBorder("Connection"));

        jlbIp.setText("IP");

        jlbPort.setText("Port");

        jlbUser.setText("User");

        jlbPass.setText("Password");

        jbtConnect.setText("Connect");

        jbtConnCancel.setText("Cancel");

        javax.swing.GroupLayout jpanelConnLayout = new javax.swing.GroupLayout(jpanelConn);
        jpanelConn.setLayout(jpanelConnLayout);
        jpanelConnLayout.setHorizontalGroup(
            jpanelConnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelConnLayout.createSequentialGroup()
                .addComponent(jlbIp, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
            .addComponent(jtxtIp, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addComponent(jlbPort, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addComponent(jtxtPort, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addComponent(jlbUser, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addComponent(jtxtUser, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addGroup(jpanelConnLayout.createSequentialGroup()
                .addComponent(jlbPass)
                .addContainerGap())
            .addComponent(jpassConn, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
            .addGroup(jpanelConnLayout.createSequentialGroup()
                .addComponent(jbtConnect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtConnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
        );
        jpanelConnLayout.setVerticalGroup(
            jpanelConnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanelConnLayout.createSequentialGroup()
                .addComponent(jlbIp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlbPort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlbUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlbPass)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpassConn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jpanelConnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtConnect)
                    .addComponent(jbtConnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpanelConn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpanelConn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtConnCancel;
    private javax.swing.JButton jbtConnect;
    private javax.swing.JLabel jlbIp;
    private javax.swing.JLabel jlbPass;
    private javax.swing.JLabel jlbPort;
    private javax.swing.JLabel jlbUser;
    private javax.swing.JPanel jpanelConn;
    private javax.swing.JPasswordField jpassConn;
    private javax.swing.JTextField jtxtIp;
    private javax.swing.JTextField jtxtPort;
    private javax.swing.JTextField jtxtUser;
    // End of variables declaration//GEN-END:variables

    //Ouvinte de ações da Frame
    class ConnAction implements ActionListener {

        //Ação dos botões
        public void actionPerformed(ActionEvent e) {

            //Caso o botão seja para conectar
            if ( e.getSource() == jbtConnect ) {
                try {
                    main.getConn().setIP(jtxtIp.getText());
                    main.getConn().setPort(Integer.valueOf(jtxtPort.getText()));
                    main.getConn().setUser(jtxtUser.getText());
                    main.getConn().start(jpassConn.getText());
                    main.printPrompt("Connected.");
                    dispose();
                } catch (SQLException ex) {
                    main.printException(ex);
                    JOptionPane.showMessageDialog(null,"Exception returned by SGBD.");
                } catch (NullConnection ex) {
                    main.printException(ex);
                    JOptionPane.showMessageDialog(null,"Database connection does not exist.");
                } catch (ClassNotFoundException ex) {
                    main.printException(ex);
                    JOptionPane.showMessageDialog(null,"JDBC Connector not found.");
                }
            }
            //Caso o botão seja para fechar a janela
            if ( e.getSource() == jbtConnCancel ) {
               dispose();
            }

        }

    }

}
